<?php
	//Load model
	require_once ($_SERVER['DOCUMENT_ROOT']. "/_config/config.php");
	require_once (MODEL_DIR."/product_model.php");
	require_once (HELPER_DIR."/helper.php");
	
	
	$keyword = str_replace(array("'", '"'), "", $_GET['q']);
	//$keyword = strtolower($keyword);
	$keyword = trim($keyword);
	
	//Xu ly
	if(isset($_GET['cse']))
	{
		$product_arr = null;
		$numProduct =0;
		$numPage = 0;
		//render
		require_once('search_view.phtml');
		exit();
	}
	
	if(strlen($keyword) >= 3)
	{
		$product_arr = GetProductSearch(0, PAGING_PRODUCT_PER_PAGE, '', '', $keyword);
		//var_dump($product_arr); die();
		if(!$product_arr)
		{
			$q = str_replace(' ','+', $keyword);
			//header("Location: /sg.html?cse=1&cx=006352492000630609548:qeg6h_5tmnu&cof=FORID%3A10&ie=UTF-8&q=$q");exit;
			//header("Location: /sg-$keyword.html?cse=1&cx=004157100465853739646%3Ah-flcr4tzlg&cof=FORID%3A10&ie=UTF-8&q=$keyword");*/
			$product_arr = null;
		}
		
		
		$numProduct = GetNumProductSearch('','', $keyword);
		
	}
	else
	{
		$product_arr = null;
		$numProduct =0;
	}
		
	$numPage = ceil(floatval($numProduct)/PAGING_PRODUCT_PER_PAGE);
	
	
	//render
	require_once('search_view.phtml');
	
?>